<?php
declare(strict_types=1);

namespace App\Logic\User\Service\Image;

use App\Library\SnowFlakeId;
use App\Logic\User\Repository\Image\ImageCollectionRepository;
use App\Logic\User\Service\BaseService;
use App\Logic\User\Service\UserServiceInterface;
use Closure;

class ImageCollectionService extends BaseService implements UserServiceInterface
{

    public static function searchWhere(array $requestParams): Closure
    {
        return function ($query) use ($requestParams) {
            extract($requestParams);
            if (!empty($author_uid)) {
                $query->where("author_uid", "=", $author_uid);
            }
            if (!empty($user_uid)) {
                $query->where("user_uid", "=", $user_uid);
            }
            if (!empty($image_uid)) {
                $query->where("image_uid", "=", $image_uid);
            }
        };
    }

    public function serviceSelect(array $requestParams): array
    {
        $requestParams["user_uid"] = $this->getUserUid();
        return (new ImageCollectionRepository())->repositorySelect(self::searchWhere($requestParams),
            (int)($requestParams["size"]),
            ["image_uid", "created_at as attention_time"]
        );
    }

    public function serviceCreate(array $requestParams): array
    {
        $imageCollectionRepository = new ImageCollectionRepository();
        $requestParams["user_uid"] = $this->getUserUid();
        $requestParams["uid"] = SnowFlakeId::getId();
        if ($imageCollectionRepository->repositoryCreate($requestParams)) {
            return ["code" => 0, "msg" => "收藏成功"];
        }
        return ["code" => 1, "msg" => "收藏失败"];

    }

    public function serviceUpdate(array $requestParams): array
    {
        return [];
    }

    public function serviceDelete(array $requestParams): int
    {
        return 0;
    }

    public function serviceFind(array $requestParams): array
    {
        return [];
    }
}
